package com.etc.controller;

import com.etc.core.CodeMessage;
import com.etc.pojo.Authority;
import com.etc.pojo.CustomPo;
import com.etc.pojo.UerInfomst;
import com.etc.service.UserInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@RestController
public class UserInfoController {
    @Autowired
    UserInfoService service;

    @GetMapping("api/getAllUserinfo")
    public CodeMessage getAll(@RequestParam("pageNo") Integer pageNo) {

        if (pageNo == null) {
            pageNo = 1;
        }
        // 设置好第几页和 总页数
        PageHelper.startPage(pageNo, 10);


        List<UerInfomst> plist = service.findAllUserInfo();

        // 包装成翻页模型
        PageInfo<UerInfomst> pi = new PageInfo(plist);

        CodeMessage cm = new CodeMessage();
        cm.setCode("200");
        cm.setMessage("hello spring boot");

        cm.setData(pi);

        return cm;
    }



    @GetMapping("api/getAllUserinfo1")
    public CodeMessage getAll1(@Valid Authority a, BindingResult result) {
        CodeMessage cm = new CodeMessage();
        if (result.hasFieldErrors()) {
            cm.setCode("201");
            StringBuilder error = new StringBuilder();
            result.getAllErrors().forEach( msg -> {
                error.append(msg.getDefaultMessage());
            });
            cm.setMessage(error.toString());
            return cm;
        }


        System.out.println(a);

        cm.setCode("200");
        cm.setMessage("hello spring boot");


        return cm;
    }
}
